package com.hbm.usercenter.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbm.usercenter.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by BS on 2017/6/2.
 */

public class GroupInput extends LinearLayout {

    @Bind(R.id.tv_text)
    TextView mTvText;
    @Bind(R.id.et_input)
    EditText mEtInput;
    @Bind(R.id.h_line)
    View mHLine;

    public GroupInput(Context context) {
        super(context);
        initView(context);
    }

    public GroupInput(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initTypedArray(context, attrs);
    }

    public GroupInput(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initTypedArray(context, attrs);
    }

    private void initView(Context context) {
        if (!isInEditMode()) {
            View view = LayoutInflater.from(context).inflate(R.layout.group_input, this, true);
            ButterKnife.bind(this, view);
        }
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupInput);

        if (null != typedArray) {

            String gourp_desc = typedArray.getString(R.styleable.GroupInput_group_desc);
            String group_hint = typedArray.getString(R.styleable.GroupInput_group_hint);
            //是否有分割线
            boolean isLine = typedArray.getBoolean(R.styleable.GroupInput_group_is_line, true);
            //是否为密码类型
            boolean isPwd = typedArray.getBoolean(R.styleable.GroupInput_group_is_pwd, false);

            if (!isInEditMode()) {
                //设置TextView的文字
                if (!TextUtils.isEmpty(gourp_desc)) {
                    mTvText.setText(gourp_desc);
                }
                //设置EditText的提示
                if (!TextUtils.isEmpty(group_hint)) {
                    mEtInput.setHint(group_hint);
                }
                //是否显示下划线
                if (mHLine != null) {
                    if (isLine) {
                        mHLine.setVisibility(View.VISIBLE);
                    } else {
                        mHLine.setVisibility(View.INVISIBLE);
                    }
                }
                //输入类型是否为密码类型
                if (isPwd) {
                    mEtInput.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo
                            .TYPE_TEXT_VARIATION_PASSWORD);
                }

            }
        }
        typedArray.recycle();
    }

    //获取输入的内容
    public String getText() {
        return mEtInput.getText().toString().trim();
    }
}
